package cn.xiaolang.function.test_map;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author fuhw/Dean
 * @date 2020-03-20
 */
public class LruCacheMap<K, V> extends LinkedHashMap<K, V> {

    private int capacity;

    public LruCacheMap(int capacity) {
        super((int) Math.ceil(capacity / 0.75) + 1, 0.75f, true);
        this.capacity = capacity;
    }

    @Override
    protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
        return size() > capacity;
    }


    public static void main(String[] args) {
        LruCacheMap<String, Integer> lruCacheMap = new LruCacheMap<>(3);
        System.out.println(lruCacheMap.size());
        lruCacheMap.put("A", 1);
        lruCacheMap.put("B", 2);
        lruCacheMap.put("C", 3);
        lruCacheMap.get("A");
        lruCacheMap.put("D", 4);
        lruCacheMap.put("E", 5);

        System.out.println(lruCacheMap);
    }
}
